import {
  imageUrl
} from "../utils/imageutil"
Page({

  data: {
    imageUrl,
    id: '',
    pay_info: null,
    list: [],
    list_index: -1,
    phone: '',
    code: '',
    order_id: '',
    code_txt: '发送验证码',
    t: 60,
    time: 60,
    old_index: -1,
    show_popup: false,
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
      })
    }
    if(wx.getStorageSync('pay_info')){
      this.setData({
        pay_info: wx.getStorageSync('pay_info')
      })
    }
  },

  onShow() {
    this.get_list()
  },

  add() {
    wx.navigateTo({
      url: '/online_bank/add'
    })
  },
  get_list() {
    getApp().globalData.post({
      url: '/api/applet.pay/getsigncard.html'
    }, true).then(res => {
      this.setData({
        list: res.data.data
      })
    })
  },
  smsPay() {
    getApp().globalData.post({
      url: '/api/applet.pay/smsPay.html',
      data: {
        code: this.data.code,
        order: this.data.order_id
      }
    }, true).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        this.setData({
          show_popup: false
        })
        if (res.data.code == 1) {
          let pages = getCurrentPages()
          let perpage = pages[pages.length - 2]
          perpage.setData({
            has_jump: true,
            is_bank: true
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 200)
        }
      })
    })
  },
  change_code(e) {
    this.setData({
      code: e.detail.value
    })
  },
  close() {
    this.setData({
      show_popup: false
    })
  },
  get_bank(e) {
    this.setData({
      list_index: e.currentTarget.dataset.index
    })
  },
  get_pay_code() {
    if (this.data.time == this.data.t) {
      if (this.data.list_index == -1) {
        getApp().showModal('请先选择银行卡')
        return
      }
      if (this.data.list_index != this.data.old_index) {
        this.setData({
          time: this.data.t
        })
      }
      this.to_pay()
    } else {
      if (this.data.list_index == this.data.old_index) {
        this.setData({
          show_popup: true
        })
      } else {
        this.setData({
          time: this.data.t
        })
        this.to_pay()
      }
    }

  },

  to_pay() {
    getApp().showModal('是否使用该银行卡支付', true).then(() => {
      getApp().globalData.post({
        data: {
          ...this.data.pay_info,
          bank_id: this.data.list[this.data.list_index].bank_id
        }
      }).then(res => {
        getApp().showModal(res.data.msg).then(() => {
          if (res.data.pay_status == 1) {
            this.setData({
              old_index: this.data.list_index,
              order_id: res.data.order_id,
              phone: this.data.list[this.data.list_index].phone,
              show_popup: true
            })
            let time1 = setInterval(() => {
              if (this.data.time == 0) {
                this.setData({
                  code_txt: '发送验证码',
                  time: this.data.t,
                })
                clearInterval(time1);
              } else {
                this.setData({
                  code_txt: this.data.time + "秒后可重新获取",
                  time: this.data.time - 1
                })
              }
            }, 1000)
          }
        })
      })
    })
  },

})